<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html><head><title>Programming in Lua : 2</title>
<link rel="stylesheet" type="text/css" href="pil.css">
</head>
<body>
<p class="warning">
<A HREF="contents.html"><IMG TITLE="Programming in Lua (first edition)" SRC="capa.jpg" ALT="" ALIGN="left"></A>This first edition was written for Lua 5.0. While still largely relevant for later versions, there are some differences.<BR>The third edition targets Lua 5.2 and is available at <A HREF="http://www.amazon.com/exec/obidos/ASIN/859037985X/theprogrammil3-20">Amazon</A> and other bookstores.<BR>By buying the book, you also help to <A HREF="../donations.html">support the Lua project</A>.
</p>
<table width="100%" class="nav">
<tr>
<td width="10%" align="left"><a href="1.4.html"><img border="0" src="left.png" alt="Previous"></a></td>
<td width="80%" align="center"><a href="contents.html"><font face="Helvetica,Arial,sanserif">
<font color="gray">Programming in </font><font color="blue"> Lua</font>
</font></a></td>
<td width="10%" align="right"><a href="2.1.html"><img border="0" src="right.png" alt="Next"></a></td>
</tr>
<tr>
<td width="10%" align="left"></td>
<td width="80%" align="center"><a href="contents.html#P1">Part I. The Language</a>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<a href="contents.html#2">Chapter 2. Types and Values</a></td>
<td width="10%" align="right"></td></tr>
</table>
<hr/>
<a name="TypesSec"><h1>2 &ndash; Types and Values</h1></a>

<p>Lua is a dynamically typed language.
There are no type definitions in the language;
each value carries its own type.

<p>There are eight basic types in Lua:
<em>nil</em>, <em>boolean</em>, <em>number</em>, <em>string</em>, <em>userdata</em>,
<em>function</em>, <em>thread</em>, and <em>table</em>.
The <code>type</code> function gives
the type name of a given value:
<pre>
    print(type("Hello world"))  --> string
    print(type(10.4*3))         --> number
    print(type(print))          --> function
    print(type(type))           --> function
    print(type(true))           --> boolean
    print(type(nil))            --> nil
    print(type(type(X)))        --> string
</pre>
The last example will result in <code>"string"</code> no matter the value of <code>X</code>,
because the result of <code>type</code> is always a string.

<p>Variables have no predefined types;
any variable may contain values of any type:
<pre>
    print(type(a))   --> nil   (`a' is not initialized)
    a = 10
    print(type(a))   --> number
    a = "a string!!"
    print(type(a))   --> string
    a = print        -- yes, this is valid!
    a(type(a))       --> function
</pre>
Notice the last two lines:
Functions are first-class values in Lua;
so, we can manipulate them like any other value.
(More about that in <a href="6.html#firstclass">Chapter 6</a>.)

<p>Usually,
when you use a single variable for different types,
the result is messy code.
However, sometimes the judicious use of this facility
is helpful,
for instance in the use of <B>nil</B> to differentiate a normal
return value from an exceptional condition.

<hr/>
<table width="100%" class="nav">
<tr valign="top">
<td width="90%" align="left">
<small>
  Copyright &copy; 2003&ndash;2004 Roberto Ierusalimschy.  All rights reserved.
</small>
</td>
<td width="10%" align="right"><a href="2.1.html"><img border="0" src="right.png" alt="Next"></a></td>
</tr>
</table>


</body></html>

